package com.carassistant.beta.common;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.carassistant.beta.R;

/**
 * 所有的Activity继承该类，便于管理所有的Activity
 * 
 * @author Alen
 * */
public class CarAssistantActivity extends ActivityGroup implements
		OnClickListener {
	public static final int HEAD_HAVE_BACK = 0;
	public static final int HEAD_HAVE_MORE = 1;

	private RelativeLayout headerInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AppManager.getInstance().addActivity(this);
	}

	protected ViewGroup getHeaderInstance(int HOME_TYPE, String titleMessage) {
		if (headerInstance == null) {
			headerInstance = addHeader(HOME_TYPE, titleMessage);
		}
		return headerInstance;
	}

	/**
	 * 界面顶部header
	 * 
	 * @param HOME_TYPE
	 *            控制更多button是否显示
	 * @param titleMessage
	 *            要显示的内容
	 * @return 返回view布局
	 */
	private RelativeLayout addHeader(int HOME_TYPE, final String titleMessage) {
		RelativeLayout headView = (RelativeLayout) LayoutInflater.from(this)
				.inflate(R.layout.head_view, null);
		RelativeLayout.LayoutParams headParams = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, dip2px(50));
		headParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		headView.setLayoutParams(headParams);

		ImageButton backBtn = (ImageButton) headView
				.findViewById(R.id.back_home_btn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				backHomeScreen();
			}
		});

		if (titleMessage != null) {
			TextView titleTv = (TextView) headView.findViewById(R.id.titleTv);
			titleTv.setText(titleMessage);
		}

		if (HOME_TYPE == HEAD_HAVE_MORE) {
			ImageButton moreBtn = (ImageButton) headView
					.findViewById(R.id.more_btn);
			moreBtn.setVisibility(View.VISIBLE);
		}

		return headView;
	}

	/**
	 * finish除了主activity的所有activity
	 * */
	protected void backHomeScreen() {
		AppManager.getInstance().backHomeActivity();
	}

	/**
	 * 判断Activity是否打开
	 * */
	protected boolean isActvityHaveOpen(Class<?> sub) {
		return AppManager.getInstance().isActvityHaveOpen(sub);
	}

	/**
	 * 跳转Activity
	 * */
	protected void goOtherActivity(Class<?> sub) {
		startActivity(new Intent(this, sub));
	}

	/**
	 * 关闭音乐服务
	 * */
	protected void stopMusicPlayer() {
		/*
		 * if (CarStreamApp.isSerivceRunning(this,
		 * CarStreamApp.MUSIC_SERVICE_NAME)) { stopService(new
		 * Intent(MusicTools.MUSIC_SERVICE_STOP_INTENT)); }
		 */
	}

	protected float getSize(float value) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value,
				getResources().getDisplayMetrics());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	/**
	 * 根据手机分辨率从dp转成px
	 * 
	 * @param context
	 * @param dpValue
	 * @return
	 */
	protected int dip2px(float dpValue) {
		final float scale = this.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	protected int px2dip(float pxValue) {
		final float scale = this.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f) - 15;
	}
}
